<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@page import="java.util.*"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎使用代码管理</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/code_gen.css" type="text/css">
<link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css"
	type="text/css">
<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="js/jquery.ztree.exedit.js"></script>
<jsp:useBean id="UI" class="cn.iutils.code.main.Ajax" scope="page" />
<SCRIPT type="text/javascript">
//<!--
	var setting = {
		async: {
			enable: true,
			url: getUrl
		},
		view : {
			dblClickExpand : false,
			expandSpeed: ""
		},
		check : {
			enable : true
		},
		callback : {
			onRightClick : OnRightClick,
			beforeExpand: beforeExpand,
			onAsyncSuccess: onAsyncSuccess,
			onAsyncError: onAsyncError
		}
	};

	var zNodes = [
		<%
		List<String> dbs=UI.getDatabase();
		for(int idx=0;idx<dbs.size();idx++){
		if(idx==0){%>		
		{
		id : <%=idx%>,
		name : "<%=dbs.get(idx)%>",
		open : false,
		noR : false, isParent:true
		}
		<%}else{%>
		,{
			id : <%=idx%>,
			name : "<%=dbs.get(idx)%>",
			open : false,
			noR : false, isParent:true
		}
		<%}
		}%>
		];

	startTime = 0, endTime = 0, perCount = 100, perTime = 100;
	function getUrl(treeId, treeNode) {
		var curCount = (treeNode.children) ? treeNode.children.length : 0;
		var dbName = treeNode.name;
		var param = "action=gettable&db="+dbName+"&_"+(treeNode.times++);
		return "ajax.jsp?" + param;
	}
	function beforeExpand(treeId, treeNode) {
		if (!treeNode.isAjaxing) {
			startTime = new Date();
			treeNode.times = 1;
			ajaxGetNodes(treeNode, "refresh");
			return true;
		} else {
			alert("正在下载数据中，请稍后展开节点。。。");
			return false;
		}
	}
	function onAsyncSuccess(event, treeId, treeNode, msg) {
		if (!msg || msg.length == 0) {
			return;
		}
		var zTree = $.fn.zTree.getZTreeObj("treeDatabase"),
		totalCount = treeNode.count;
		if (treeNode.children.length < totalCount) {
			setTimeout(function() {ajaxGetNodes(treeNode);}, perTime);
		} else {
			treeNode.icon = "";
			zTree.updateNode(treeNode);
			zTree.selectNode(treeNode.children[0]);
		}
	}
	function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
		var zTree = $.fn.zTree.getZTreeObj("treeDatabase");
		alert("异步获取数据出现异常。");
		treeNode.icon = "";
		zTree.updateNode(treeNode);
	}
	function ajaxGetNodes(treeNode, reloadType) {
		var zTree = $.fn.zTree.getZTreeObj("treeDatabase");
		if (reloadType == "refresh") {
			treeNode.icon = "css/zTreeStyle/img/loading.gif";
			zTree.updateNode(treeNode);
		}
		zTree.reAsyncChildNodes(treeNode, reloadType, true);
	}
	
	function OnRightClick(event, treeId, treeNode) {
		if (!treeNode && event.target.tagName.toLowerCase() != "button"
				&& $(event.target).parents("a").length == 0) {
			zTree.cancelSelectedNode();
			showRMenu("root", event.clientX, event.clientY);
		} else if (treeNode && !treeNode.noR) {
			zTree.selectNode(treeNode);
			showRMenu("node", event.clientX, event.clientY);
		}
	}

	function showRMenu(type, x, y) {
		$("#rMenu ul").show();
		if (type == "root") {
			$("#m_del").hide();
			$("#m_check").hide();
			$("#m_unCheck").hide();
		} else {
			$("#m_del").show();
			$("#m_check").show();
			$("#m_unCheck").show();
		}
		rMenu.css({
			"top" : y + "px",
			"left" : x + "px",
			"visibility" : "visible"
		});

		$("body").bind("mousedown", onBodyMouseDown);
	}
	function hideRMenu() {
		if (rMenu)
			rMenu.css({
				"visibility" : "hidden"
			});
		$("body").unbind("mousedown", onBodyMouseDown);
	}
	function onBodyMouseDown(event) {
		if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
			rMenu.css({
				"visibility" : "hidden"
			});
		}
	}
	function genTreeNode() {
		hideRMenu();
		if(!$("#txtPackage").val()){
			alert("请填写包名称");
			$("#txtPackage").focus();
			return;
		}
		if(!$("#txtModule").val()){
			alert("请填写模块名称");
			$("#txtModule").focus();
			return;
		}
		var zTree = $.fn.zTree.getZTreeObj("treeDatabase");
		var selectedNode = zTree.getCheckedNodes();
		var sValue="";
		for(var i=0;i<selectedNode.length;i++){
			var pNode=selectedNode[i].getParentNode();
			if(pNode){
				sValue+=pNode.name+","+selectedNode[i].name+"|";
			}
		}
		if(!sValue){
			alert("请勾选相关表");
			return;
		}
		$("#txtSelDBTab").val(sValue);
		$("#formGenData").submit();
	}
	function removeTreeNode() {
		hideRMenu();
		var nodes = zTree.getSelectedNodes();
		if (nodes && nodes.length > 0) {
			if (nodes[0].children && nodes[0].children.length > 0) {
				var msg = "要隐藏的节点是父节点，如果隐藏将连同子节点一起隐藏\r\n可刷新恢复，或点击右键恢复。\n\n请确认！";
				if (confirm(msg) == true) {
					zTree.removeNode(nodes[0]);
				}
			} else {
				zTree.removeNode(nodes[0]);
			}
		}
	}
	function checkTreeNode(checked) {
		var nodes = zTree.getSelectedNodes();
		if (nodes && nodes.length > 0) {
			zTree.checkNode(nodes[0], checked, true);
		}
		hideRMenu();
	}
	function resetTree() {
		hideRMenu();
		$.fn.zTree.init($("#treeDatabase"), setting, zNodes);
	}

	var zTree, rMenu;
	$(document).ready(function() {
		$.fn.zTree.init($("#treeDatabase"), setting, zNodes);
		zTree = $.fn.zTree.getZTreeObj("treeDatabase");
		rMenu = $("#rMenu");
		$(".content_wrap ").height($(window).height()-55);
	});
//-->
</SCRIPT>
</HEAD>

<BODY>
	<h1>代码生成</h1>
	<div class="content_wrap">
		<div class="ztreeDatabaseBackground left">
			<ul id="treeDatabase" class="ztree"></ul>
		</div>
		<form action="ajax.jsp?action=gendata" id="formGenData" method="post">
		<input type="hidden" name="txtSelDBTab" id="txtSelDBTab" value="">
		<div class="right">
			<ul class="info">
				<li class="title"><h2>生成相关参数</h2>				
					<ul class="list">
						<li><label for="txtPackage">包名称*</label><input type="text"
							name="txtPackage" value="<%=UI.getDefPackage() %>" id="txtPackage"></li>
						<li><label for="txtFilter">筛选字符</label><input type="text"
							name="txtFilter" value="<%=UI.getDefFilter()%>" id="txtFilter"></li>
						<li><label for="txtModule">模块名*</label><input type="text"
							name="txtModule" id="txtModule"></li>
						<li><label for="txtSubModule">子模块名</label><input type="text"
							name="txtSubModule" id=""txtSubModule""></li>
					</ul></li>
				<div id="divbtns">
					<a href="javascript:void(0)" onclick="genTreeNode()" class="btn">生成代码</a>
				</div>
			</ul>
		</div>
		</form>
	</div>
	<div id="rMenu">
		<ul>
			<li id="m_gen" class="Underline" onclick="genTreeNode();">生成已选择</li>
			<li id="m_del" onclick="removeTreeNode();">隐藏节点</li>
			<li id="m_check" onclick="checkTreeNode(true);">选中节点</li>
			<li id="m_unCheck" onclick="checkTreeNode(false);">取消选中节点</li>
			<li id="m_reset" onclick="resetTree();">恢复</li>
		</ul>
	</div>
</BODY>
</HTML>